﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using System.Data.Services.Client;
using iTraffic_ClientWP.iTrafficService;
using System.Windows.Media.Imaging;
using Microsoft.Phone.Tasks;
using Microsoft.Xna.Framework.Media;
using System.IO.IsolatedStorage;
using System.IO;
using Microsoft.Phone;
using System.Collections.Specialized;
using System.Net;
using System.IO;
using System.Device.Location;
using System.Text;
using System.Xml.Linq;
using Microsoft.Phone.Controls.Maps;
using Microsoft.Phone.Controls.Maps.Core;
using iTraffic_ClientWP.Model;
namespace iTraffic_ClientWP
{
    public partial class ViewPhotos : PhoneApplicationPage
    {

        int pointId;
        static string serviceUriString = UrlConfig.SERVICE_URI_STRING;
        private Uri svcRootUri = new Uri(serviceUriString, UriKind.Absolute);

        // Declare our private binding collection.
        private DataServiceCollection<HotPoints_Photos> _photos;
        private DataServiceCollection<HotPoints> _hotpoint;
        // Declare the DataServiceContext as private.
        private iTrafficEntities _context;
        public ViewPhotos()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
           // base.OnNavigatedTo(e);
            string pointIdAsString = this.NavigationContext.QueryString["pointId"];
            pointId = int.Parse(pointIdAsString);

            this.loadPhotosByHotpointId();
            this.getInfoHotPoint();
        }

        public void loadPhotosByHotpointId() {
            _context = new iTrafficEntities(svcRootUri);
            _photos = new DataServiceCollection<HotPoints_Photos>(_context);

            // Load the data from the Photo feed.
            _photos.LoadAsync(new Uri(UrlConfig.GET_PHOTOS_BY_HOT_POINT + pointId, UriKind.RelativeOrAbsolute));

            // Register for the LoadCompleted event to turn off the progress bar.
            _photos.LoadCompleted +=
                new EventHandler<LoadCompletedEventArgs>(Photos_LoadCompleted);
        }

        void Photos_LoadCompleted(object sender, System.Data.Services.Client.LoadCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                // Make sure that we load all pages of the Customers feed.
                if (_photos.Continuation != null)
                {
                    // Request the next page from the data service.
                    _photos.LoadNextPartialSetAsync();
                }
                else
                {
                    List<PhotosByPointId> photoList = new List<PhotosByPointId>();
                    for (int i = 0; i < _photos.Count; i++)
                    {
                        HotPoints_Photos info = _photos[i];
                        Uri uri = new Uri(UrlConfig.GET_THUMBNAIL + info.FileName);
                        photoList.Add(new PhotosByPointId(uri.ToString(), info.FileName, info.Id));
                    }
                    //put image
                    photosByPointId.ItemsSource = photoList;

                }
               // this.requestProgress.Visibility = Visibility.Collapsed;
                //this.requestProgress.IsIndeterminate = false;
            }
            else
            {
                if (MessageBox.Show(e.Error.Message, "Retry request?",
                    MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    //    this.LoadData();
                }
            }
        }

        /*---------------------------------------- Get Hot Point By ID-----------------------*/
        private void getInfoHotPoint()
        {

            // Load data from the data service.
            _context = new iTrafficEntities(svcRootUri);

            // Call Add Header to Request
            // _context.SendingRequest += new EventHandler<SendingRequestEventArgs>(OnSendingRequest);

            _hotpoint = new DataServiceCollection<HotPoints>(_context);

            // Load the data from the Photo feed.
            _hotpoint.LoadAsync(new Uri(UrlConfig.GET_HOT_POINT_BY_ID, UriKind.Absolute));

            // Register for the LoadCompleted event to turn off the progress bar and load Thumbnail.
            _hotpoint.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(GetHotPoint_LoadCompleted);
        }

        private void GetHotPoint_LoadCompleted(object sender, LoadCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                // Make sure that we load all pages of the Photo feed.
                if (_hotpoint.Continuation != null)
                {
                    // Request the next page from the data service.
                    _hotpoint.LoadNextPartialSetAsync();
                }
                else
                {

                    List<HotPoint> _pointList = new List<HotPoint>();
                    for (int i = 0; i < _hotpoint.Count; i++)
                    {
                        HotPoints info = _hotpoint[i];
                        _pointList.Add(new HotPoint(info.Id, info.Name, info.Longitude, info.Latitude));
                        PageTitle.Text = info.Name;
                    }              

                }

            }
            else
            {
                if (MessageBox.Show(e.Error.Message, "Retry request?",
                    MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {

                }
            }
        }

    }
}